package com.problem.leetcode;


public class Problem345 {
    public String reverseVowels(String s) {
        if (s == null) {
            return null;
        }
        char[] values = s.toCharArray();
        int    begin  = 0, end = s.length() - 1;
        while (begin < end) {

            int begin1 = findBegin(values, begin, end);
            if (begin1 == -1) {
                break;
            }

            int end1 = findEnd(values, begin1, end);
            if (end1 == -1) {
                break;
            }
            swap(values, begin1, end1);
            begin = ++begin1;
            end = --end1;
        }
        return String.valueOf(values);
    }

    private int findBegin(char[] strs, int begin, int end) {
        while (begin < end) {
            if (isRight(strs[begin])) {
                return begin;
            } else {
                ++begin;
            }
        }
        return -1;
    }

    private int findEnd(char[] strs, int begin, int end) {
        while (begin < end) {
            if (isRight(strs[end])) {
                return end;
            } else {
                --end;
            }
        }
        return -1;
    }

    private void swap(char[] strs, int begin, int end) {
        char tmp = strs[begin];
        strs[begin] = strs[end];
        strs[end] = tmp;
    }

    private boolean isRight(char ele) {
        if (ele == 'a' || ele == 'e' || ele == 'i' || ele == 'o' || ele == 'u' || ele == 'A' ||
            ele == 'E' || ele == 'I' || ele == 'O' || ele == 'U') {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String result = new Problem345().reverseVowels("leotcede");
        System.out.println(result);
    }

}
